package com.cloudsigns.factorymanager2.project.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.bean.OrderParentBean;
import com.cloudsigns.factorymanager2.project.myinerface.ItemClickListener;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 工程工单列表 适配器
 * Created by tony on 2017/12/12.
 */

public class EngineeringOrderListAdapter extends BaseAdapter {

    private Context context;
    private List<OrderParentBean> datas = new ArrayList<OrderParentBean>();
    private LayoutInflater inflater;
    private ItemClickListener clickListener;
    private String TAG = "";


    public EngineeringOrderListAdapter(Context context, String TAG) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.TAG = TAG;

    }

    public void setData(List<OrderParentBean> list) {

        if (null != list) {
            this.datas.clear();
            this.datas.addAll(list);
        }

    }

    public void setItemClick(ItemClickListener clickListener) {
        this.clickListener = clickListener;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public OrderParentBean getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();

            convertView = inflater.inflate(R.layout.item_engineering_order_list_lv, null);
            holder.tv_top = convertView.findViewById(R.id.tv_top);
            holder.tv_bottom = convertView.findViewById(R.id.tv_bottom);
            holder.tv_status = convertView.findViewById(R.id.tv_status);

            convertView.setTag(holder);
        } else {
            holder = (EngineeringOrderListAdapter.ViewHolder) convertView.getTag();
        }

        OrderParentBean bean = datas.get(position);
        if (bean.getCategory() == CommonEnum.EngineeringORderCategory.INSTALL) {
            holder.tv_top.setText("工程安装");
            holder.tv_bottom.setText(bean.getProjectName() + "/" + bean.getEngineeringName() + "/" + DateUtil.getYyyy_Mm_Dd(bean.getCreateDate()));
        } else if (bean.getCategory() == CommonEnum.EngineeringORderCategory.CONSTRUCT) {
            holder.tv_top.setText("工程施工");
            holder.tv_bottom.setText(bean.getProjectName() + "/" + bean.getEngineeringName() + "/" + DateUtil.getYyyy_Mm_Dd(bean.getCreateDate()));
        } else if (bean.getCategory() == CommonEnum.EngineeringORderCategory.BATCH_INSTALL) {
            holder.tv_top.setText("批量安装");
            holder.tv_bottom.setText(bean.getProjectName() + "/" + bean.getEngineeringName() + "/" + DateUtil.getYyyy_Mm_Dd(bean.getCreateDate()));
        } else if (bean.getCategory() == CommonEnum.EngineeringORderCategory.MAINTAIN) {
            holder.tv_top.setText("维修");
            holder.tv_bottom.setText(bean.getProjectName() + "/" + bean.getEngineeringName() + "/" + DateUtil.getYyyy_Mm_Dd(bean.getCreateDate()));
        } else if (bean.getCategory() == CommonEnum.EngineeringORderCategory.PUBLICATION) {
            holder.tv_top.setText("广告上刊");
            holder.tv_bottom.setText(bean.getProjectName() + "/" + bean.getEngineeringName() + "/" + DateUtil.getYyyy_Mm_Dd(bean.getCreateDate()));
        } else if (bean.getCategory() == CommonEnum.EngineeringORderCategory.PATROL) {
            holder.tv_top.setText("巡查");
            holder.tv_bottom.setText(bean.getProjectName() + "/" + bean.getEngineeringName() + "/" + DateUtil.getYyyy_Mm_Dd(bean.getCreateDate()));
        } else if (bean.getCategory() == CommonEnum.EngineeringORderCategory.SURVEY) {
            holder.tv_top.setText("现场勘察");
            holder.tv_bottom.setText(bean.getProjectName() + "/" + bean.getEngineeringName() + "/" + DateUtil.getYyyy_Mm_Dd(bean.getCreateDate()));
        }

        if (TAG.equals("SuccessOrderListForMnagerActivity") || TAG.equals("CompletionOrderListForSuperActivity")) {
            CommonEnum.CompleteStatus completeStatus = bean.getCompleteStatus();
            holder.tv_status.setTextColor(context.getResources().getColor(R.color.rgb_FFC000));
            if (null != completeStatus) {
                if (completeStatus == CommonEnum.CompleteStatus.WORKING) {
                    holder.tv_status.setText("");

                } else if (completeStatus == CommonEnum.CompleteStatus.WORKER_COMPLETE) {
                    holder.tv_status.setText("[等待竣工]");
                    holder.tv_status.setTextColor(context.getResources().getColor(R.color.rgb_FCD505));
                } else if (completeStatus == CommonEnum.CompleteStatus.ADMIN_COMPLETE) {
                    holder.tv_status.setText("[等待竣工]");
                    holder.tv_status.setTextColor(context.getResources().getColor(R.color.rgb_FCD505));
                } else if (completeStatus == CommonEnum.CompleteStatus.SUPERADMIN_COMPLETE) {
                    holder.tv_status.setText("[已经竣工]");
                    holder.tv_status.setTextColor(context.getResources().getColor(R.color.color_87c688));
                }
            }
        } else {
            if (bean.getStatus() == CommonEnum.EngineeringOrderStatus.CREATED) {
                holder.tv_status.setTextColor(context.getResources().getColor(R.color.rgb_FFC000));
                if (TAG.equals("EditEngineeringOrderListActivity")) {
                   // holder.tv_status.setText("[等待编辑]");
                    holder.tv_status.setText("[编辑中]");
                    holder.tv_status.setTextColor(context.getResources().getColor(R.color.rgb_FCD505));
                } else {
                    holder.tv_status.setText("");
                }


            } else if (bean.getStatus() == CommonEnum.EngineeringOrderStatus.EDITED) {
                if (TAG.equals("DispatchEngineeringOrderListActivity")) {
                    holder.tv_status.setTextColor(context.getResources().getColor(R.color.color_87c688));
                    holder.tv_status.setText("[已编辑]");
                } else if (TAG.equals("CreateEngineeringOrderListActivity")) {
                    holder.tv_status.setText("");
                } else {
                    holder.tv_status.setTextColor(context.getResources().getColor(R.color.color_87c688));
                    holder.tv_status.setText("[编辑完成]");
                }


            } else if (bean.getStatus() == CommonEnum.EngineeringOrderStatus.SEND) {
                holder.tv_status.setTextColor(context.getResources().getColor(R.color.rgb_FCD505));
                holder.tv_status.setText("[等待接收]");
            } else if (bean.getStatus() == CommonEnum.EngineeringOrderStatus.RECEIVED) {
                holder.tv_status.setTextColor(context.getResources().getColor(R.color.color_87c688));
                holder.tv_status.setText("[已经接收]");
            } else if (bean.getStatus() == CommonEnum.EngineeringOrderStatus.RETURNED) {
                holder.tv_status.setTextColor(context.getResources().getColor(R.color.rgb_C00000));
                holder.tv_status.setText("[退回]");
            } else if (bean.getStatus() == CommonEnum.EngineeringOrderStatus.COMPLETED) {
                holder.tv_status.setTextColor(context.getResources().getColor(R.color.color_87c688));
                holder.tv_status.setText("[完工]");
            } else if (bean.getStatus() == CommonEnum.EngineeringOrderStatus.CONSTRUCT) {
                holder.tv_status.setTextColor(context.getResources().getColor(R.color.rgb_FCD505));
                holder.tv_status.setText("[施工中]");
            } else if (bean.getStatus() == CommonEnum.EngineeringOrderStatus.PAUSE) {
                holder.tv_status.setTextColor(context.getResources().getColor(R.color.rgb_C00000));
                holder.tv_status.setText("[暂停]]");
            }
        }


        if (bean.isHasPauseOrder()) {
            holder.tv_status.setTextColor(context.getResources().getColor(R.color.rgb_BF9000));
            holder.tv_status.setText("[暂停]");
        }


        return convertView;
    }

    private class ViewHolder {
        private TextView tv_top;
        private TextView tv_bottom;
        private TextView tv_status;

    }
}
